package copyengine.resource.file
{
	import copyengine.resource.CEResConfig;
	import copyengine.resource.CEResManger;
	import copyengine.resource.dto.CEResInitDTOSubSwfSymbolPackge;
	import copyengine.resource.dto.LazyLoadInitDto;
	import copyengine.resource.dto.ResInitDTO;
	import copyengine.resource.dto.ResObjectRequestParamDTO;
	import copyengine.resource.lazyload.CELazyLoadCacheContainer;
	import copyengine.resource.lazyload.CELazyLoadMovieClipeContainer;
	import copyengine.resource.lazyload.CELazyLoadSpriteContainer;
	import copyengine.resource.lazyload.ICELazyLoadContainer;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.debug.CELog;

	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;

	public class CESwfResourceFile extends CEBasicResFile
	{
		/**
		 * an loader use to load  the swf file
		 */
		private var swfLoader:Loader;

		private var domain:ApplicationDomain;

		/**
		 * use in lazy load system to store all lazyLoadContainer during file load
		 */
		private var lazyLoadContainerVector:Vector.<ICELazyLoadContainer>;

		/**
		 * use in lazy load system , to recorder the swfFile property
		 */
		private var resInitDTO:ResInitDTO;

		public function CESwfResourceFile()
		{
			super();
		}

		override protected function doInitialize(_resInitDTO:ResInitDTO):void
		{
			resInitDTO = _resInitDTO;
		}

		override protected function doStartLoadFile():void
		{
			swfLoader = new Loader();
			swfLoader.load(new URLRequest(resInitDTO.filePath));
			GeneralUtils.addTargetEventListener(swfLoader.contentLoaderInfo, Event.COMPLETE, onLoaded);
			GeneralUtils.addTargetEventListener(swfLoader.contentLoaderInfo, ProgressEvent.PROGRESS, loadFileOnProgress);
			GeneralUtils.addTargetEventListener(swfLoader.contentLoaderInfo, IOErrorEvent.IO_ERROR, loadFileOnError, false, 0, true);
		}

		override protected function releaseLoader():void
		{
			if (swfLoader != null)
			{
				swfLoader.unloadAndStop(true);
				GeneralUtils.removeTargetEventListener(swfLoader.contentLoaderInfo, Event.COMPLETE, onLoaded);
				GeneralUtils.removeTargetEventListener(swfLoader.contentLoaderInfo, ProgressEvent.PROGRESS, loadFileOnProgress);
				GeneralUtils.removeTargetEventListener(swfLoader.contentLoaderInfo, IOErrorEvent.IO_ERROR, loadFileOnError);
			}
			else
			{
				CELog.err("Call release loader but loading is Null");
			}
			swfLoader = null;
		}

		public function isSymbolExist(_symbolName:String):Boolean
		{
			return getSubSwfSymbolPackgeByName(_symbolName) != null;
		}

		override public function getObject(_dto:ResObjectRequestParamDTO):Object
		{
			if (resLoadState == CEResConfig.LOAD_STATE_LOADED)
			{
				if (_dto.fileType == CEResConfig.FILE_TYPE_SWF)
				{
					var SWFClass:Class = domain.getDefinition(_dto.className) as Class;
					return new SWFClass();
				}
				else if (_dto.fileType == CEResConfig.FILE_TYPE_BITMAPDATA)
				{
					var BitmapDataClass:Class = domain.getDefinition(_dto.className) as Class;
					var bitmapData:BitmapData = new BitmapDataClass(1, 1);
					return bitmapData;
				}
				else
				{
					CELog.log("CESwfResourceFile->getObject unknow fileType : " + _dto.fileType);
					return null;
				}
			}
			else
			{
				//lazyload
				return requestLazyLoadContainer(_dto.displayObjectType, _dto.className, _dto.cacheType);
			}
			return null;
		}

		private function onLoaded(e:Event):void
		{
			//need to set the domain first , beacuse super.onLoaded(e) will send Notification
			var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
			domain = loaderInfo.applicationDomain;
			loadFileComplate();

			//first send to main system ,tell current file is been loaded then notify child container
			//because lazyLoadContainer need call main system to requet the file. so need let main
			//system know this file alrady be loaded.
			notifyLazyLoadContainer();
			releaseLoader();
		}

		private function notifyLazyLoadContainer():void
		{
			if (lazyLoadContainerVector != null)
			{
				for (var i:int = 0; i < lazyLoadContainerVector.length; i++)
				{
					lazyLoadContainerVector[i].notificationLazyLoaderTargetLoadComplate();
				}
				lazyLoadContainerVector.length = 0;
			}
			lazyLoadContainerVector = null;
		}


		private function requestLazyLoadContainer(_displayObjectType:int, _className:String, _cacheType:int):ICELazyLoadContainer
		{
			if (lazyLoadContainerVector == null)
			{
				lazyLoadContainerVector = new Vector.<ICELazyLoadContainer>();
			}
			var lazyLoadInitDto:LazyLoadInitDto = new LazyLoadInitDto();

			lazyLoadInitDto.lazyLoadIcon = CEResManger.instance.getLazyLoadIcon();

			var subSwfSymbolPackge:CEResInitDTOSubSwfSymbolPackge = getSubSwfSymbolPackgeByName(_className);
			lazyLoadInitDto.swfFileHeight = subSwfSymbolPackge.swfFileHeight;
			lazyLoadInitDto.swfFileWidth = subSwfSymbolPackge.swfFileWidth;
			lazyLoadInitDto.swfFilePosX = subSwfSymbolPackge.swfFilePosX;
			lazyLoadInitDto.swfFilePosY = subSwfSymbolPackge.swfFilePosY;

			lazyLoadInitDto.fileName = resFileName;
			lazyLoadInitDto.className = _className;
			lazyLoadInitDto.cacheType = _cacheType;

			var lazyLoadContainer:ICELazyLoadContainer;
			if (_displayObjectType == CEResConfig.SWF_TYPE_SPRITE)
			{
				lazyLoadContainer = new CELazyLoadSpriteContainer();
			}
			else if (_displayObjectType == CEResConfig.SWF_TYPE_MOVIECLIPE)
			{
				lazyLoadContainer = new CELazyLoadMovieClipeContainer();
			}
			else if (_displayObjectType == CEResConfig.SWF_TYPE_CACHE)
			{
				lazyLoadContainer = new CELazyLoadCacheContainer();
			}

			if (lazyLoadContainer)
			{
				lazyLoadContainer.initialize(lazyLoadInitDto);
				lazyLoadContainerVector.push(lazyLoadContainer);
			}
			else
			{
				CELog.err('lazy load container not found for type ' + _displayObjectType);
			}

			return lazyLoadContainer;
		}

		private function getSubSwfSymbolPackgeByName(_symbolName:String):CEResInitDTOSubSwfSymbolPackge
		{
			for each (var subSymbolPackage:CEResInitDTOSubSwfSymbolPackge in resInitDTO.allSubSwfSymbolPackageVector)
			{
				if (subSymbolPackage.swfSymbolName == _symbolName)
				{
					return subSymbolPackage;
				}
			}
			return null;
		}


	}
}
